package com.guli.eduservice.controller;


import com.guli.commonutils.R;
import com.guli.eduservice.bean.chapter.ChapterVo;
import com.guli.eduservice.client.VodClient;
import com.guli.eduservice.service.EduChapterService;
import com.guli.eduservice.bean.EduChapter;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-04
 */
@Api("课程章节管理")
@RestController
@RequestMapping("/eduservice/educhapter")
@CrossOrigin
public class EduChapterController {

    @Resource
    private EduChapterService eduChapterService;

    @Resource
    private VodClient vodClient;

    //获取课程大纲
    @GetMapping("getChapterListByCourseId/{courseId}")
    public R getChapterListByCourseId(@PathVariable("courseId") String courseId){
        List<ChapterVo> chapterVos = eduChapterService.nestedList(courseId);
        return R.ok().data("chapters",chapterVos);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    //根据id查询章节
    @GetMapping("getChapterById/{id}")
    public R getChapterById(@PathVariable("id")String id){
        EduChapter eduChapter = eduChapterService.getById(id);
        return R.ok().data("eduChapter",eduChapter);
    }

    //删除章节(有小节就不删)
    @DeleteMapping("deleteChapterById/{id}")
    public R deleteChapterById(@PathVariable("id")String id){
        boolean flag = eduChapterService.deleteChapter(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }

    }
}

